﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;

namespace YArchitech.LIB
{
	public class TreeViewTools
	{
		public static TreeNode GetSelectedNode(TreeView tv, string[] name)
		{
			TreeNode treeNode = null;
			foreach (object obj in tv.Nodes)
			{
				TreeNode treeNode2 = (TreeNode)obj;
				if (name.Count<string>() > 0)
				{
					TreeNode treeNode3 = treeNode2;
					int i;
					for (i = 0; i < name.Count<string>(); i++)
					{
						if (treeNode3 != null)
						{
							treeNode = TreeViewTools.GetSelectedNode(treeNode3, name[i]);
							if (treeNode == null)
							{
								break;
							}
							treeNode3 = treeNode;
						}
					}
					if (i == name.Count<string>() && treeNode != null)
					{
						break;
					}
				}
			}
			return treeNode;
		}

		public static List<TreeNode> GetAllChildNode(TreeNode tn)
		{
			List<TreeNode> list = new List<TreeNode>();
			if (tn != null)
			{
				if (tn.Nodes == null || tn.Nodes.Count < 1)
				{
					list.Add(tn);
				}
				else
				{
					foreach (object obj in tn.Nodes)
					{
						TreeNode tn2 = (TreeNode)obj;
						list.AddRange(TreeViewTools.GetAllChildNode(tn2));
					}
				}
			}
			return list;
		}

		public static TreeNode GetSelectedNode(TreeNode tn, string name)
		{
			TreeNode treeNode = null;
			if (tn != null)
			{
				if (tn.Text == name && tn.GetNodeCount(true) == 0)
				{
					return tn;
				}
				foreach (object obj in tn.Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj;
					if (treeNode2.Text == name)
					{
						treeNode = treeNode2;
						break;
					}
					treeNode = TreeViewTools.GetSelectedNode(treeNode2, name);
					if (treeNode != null)
					{
						break;
					}
				}
			}
			return treeNode;
		}

		public static TreeNode GetSelectedNode(TreeView tv, string name)
		{
			TreeNode treeNode = null;
			foreach (object obj in tv.Nodes)
			{
				TreeNode treeNode2 = (TreeNode)obj;
				if (treeNode2 != null)
				{
					treeNode = TreeViewTools.GetSelectedNode(treeNode2, name);
					if (treeNode != null)
					{
						break;
					}
				}
			}
			return treeNode;
		}

		public static TreeNode GetFirstNode(TreeNode tn)
		{
			TreeNode treeNode = null;
			if (tn != null)
			{
				if (tn.Nodes == null || tn.Nodes.Count < 1)
				{
					treeNode = tn;
				}
				else
				{
					foreach (object obj in tn.Nodes)
					{
						treeNode = TreeViewTools.GetFirstNode((TreeNode)obj);
						if (treeNode != null)
						{
							break;
						}
					}
				}
			}
			return treeNode;
		}

		public static TreeNode CreatTreeViewNode(string nodeText, string nodeValue)
		{
			return new TreeNode
			{
				Text = nodeText,
				Tag = nodeValue
			};
		}

		private static void tvSpaceUse_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			e.DrawDefault = true;
			if ((e.State & TreeNodeStates.Selected) != (TreeNodeStates)0)
			{
				e.Graphics.FillRectangle(Brushes.DarkBlue, e.Node.Bounds);
				Font font = e.Node.NodeFont;
				if (font == null)
				{
					font = ((TreeView)sender).Font;
				}
				e.Graphics.DrawString(e.Node.Text, font, Brushes.White, System.Drawing.Rectangle.Inflate(e.Bounds, 2, 0));
			}
			else
			{
				e.DrawDefault = true;
			}
			if ((e.State & TreeNodeStates.Focused) != (TreeNodeStates)0)
			{
				using (Pen pen = new Pen(System.Drawing.Color.Black))
				{
					pen.DashStyle = DashStyle.Dot;
					System.Drawing.Rectangle bounds = e.Node.Bounds;
					bounds.Size = new Size(bounds.Width - 1, bounds.Height - 1);
					e.Graphics.DrawRectangle(pen, bounds);
				}
			}
		}

		public static void SetTreeViewFocus(TreeView tv)
		{
			tv.HideSelection = false;
			tv.DrawMode = TreeViewDrawMode.OwnerDrawText;
			tv.DrawNode += TreeViewTools.tvSpaceUse_DrawNode;
		}
	}
}
